package com.tensquare.friend.service;

import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.pojo.Friend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.peer.FramePeer;

/**
 * Created by WF on 2019/9/24 11:10
 */
@Service
@Transactional
public class FriendService {
    @Autowired
    private FriendDao friendDao;
    //1.添加好友
    public int addFriend(String userId, String friendid) {
        //1.1)根据用户id及好友id查询是否存在此记录
        int count = friendDao.selectCount(userId,friendid);
        //1.2)如果不存在
        if(count != 0) return 0;
        //1.3)添加好友
        Friend friend = new Friend(userId,friendid);
        friend.setIslike("0");
        friendDao.save(friend);
        //1.4)查询对方是否好友是否添加
        count = friendDao.selectCount(friendid, userId);
        if(count > 0){      //如果存在此记录，就更新islike的值，双方都更新为1
            friendDao.updateIsLike("1",userId,friendid);
            friendDao.updateIsLike("1",friendid,userId);
        }
        return 1;
    }
    //删除好友
    public void delete(String friendid) {
        friendDao.deleteById(friendid);
    }
}
